package com.lm.quartz.config;

import com.lm.quartz.job.SimpleJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzManualConfig implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        // 创建 Scheduler
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        // 定义 Job
        JobDetail jobDetail = JobBuilder.newJob(SimpleJob.class)
                .withIdentity("simpleJob", "group1")
                .build();

        // 定义 Trigger
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("simpleTrigger", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(5)
                        .repeatForever())
                .build();

        // 调度 Job
        scheduler.scheduleJob(jobDetail, trigger);

        // 手动启动
        scheduler.start();

        System.out.println("✅ Quartz 已手动启动...");
    }
}
